package com.selfmade.transferutil.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.selfmade.transferutil.R;
import com.selfmade.transferutil.Transition;
import com.selfmade.transferutil.app.MyApp;
import com.selfmade.transferutil.excel.SelectExcelFile;

public class LoginActivity extends AppCompatActivity {

    private EditText userName, password;
    private Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        userName = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userName.getText().toString().equals("")) {
                    Toast.makeText(LoginActivity.this, "用户名为空", Toast.LENGTH_SHORT).show();
                } else if (password.getText().toString().equals("")) {
                    Toast.makeText(LoginActivity.this, "密码为空", Toast.LENGTH_SHORT).show();
                } else {
                    doLogin(userName.getText().toString(), password.getText().toString());
                }
            }
        });

    }

    public void doLogin(final String userName, final String password) {
        final ProgressDialog progressDialog = new ProgressDialog(this,R.style.AppTheme_Dark_Dialo);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("登录中...");
        progressDialog.show();
        progressDialog.setCanceledOnTouchOutside(false);
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                MyApp.transition = Transition.login(userName, password);
                if (MyApp.transition != null) {
                    Intent intent=new Intent();
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setClass(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    progressDialog.dismiss();
                } else {
                    Toast.makeText(LoginActivity.this, "密码或用户名错误！", Toast.LENGTH_LONG).show();
                    progressDialog.dismiss();
                }
            }},800);
    }
}
